package com.atkins.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.atkins.spring.data.IGoonData;


@Controller
@RequestMapping(value="/goon")
public class GoonController {

	@Autowired
	IGoonData dataConnection;
	
	/*@RequestMapping(value="/getList", method = RequestMethod.GET)
	public String getList(Locale locale, Model model){
		
		IGoonData.GoonList l = new IGoonData.GoonList();
		l.getList().add(new IGoonData.GoonRef(1, "John"));
		l.getList().add(new IGoonData.GoonRef(2, "Bob"));
		
		model.addAttribute("goonList", l);
		
		return "goon/list";
	}*/
	
	public class GoonViewModel{
		private IGoonData.GoonList goonList;

		public IGoonData.GoonList getGoonList() {
			return goonList;
		}

		public void setGoonList(IGoonData.GoonList goonList) {
			this.goonList = goonList;
		}
	}
	
	@RequestMapping(value="/" , method=RequestMethod.GET)
	public String main(Model model)
	{
		return "goon/main";
	}
	
	@RequestMapping(value="/getList" , method=RequestMethod.GET)
	public ModelAndView getList(Model model)
	{
		IGoonData.GoonList l = dataConnection.getGoonList();
		ModelAndView modelAndView = new ModelAndView("goon/list");
		modelAndView.addObject("goonList", l);
		return modelAndView;
	}

	@RequestMapping(value="/getDetails/{id}" , method=RequestMethod.GET)
	public ModelAndView getDetails(@PathVariable int id, Model model)
	{
		IGoonData.GoonDetails details = dataConnection.getGoonDetails(id);
		ModelAndView modelAndView = new ModelAndView("goon/details");
		modelAndView.addObject("goonDetails", details);
		return modelAndView;
	}
	

	/*@RequestMapping(value="/edit" , method=RequestMethod.POST)
	public String edit(@ModelAttribute("goon") IGoonData.GoonDetails goon, Model model)
	{
		return null;
	}*/
	
	
	// F**ing stupid. You have to provide this to instantiate the object on the form
	/*@ModelAttribute("goon")
	public IGoonData.GoonDetails stupid() {
		return new IGoonData.GoonDetails();
	}*/
	
	@RequestMapping(value="/edit" , method=RequestMethod.POST)
	public @ResponseBody PostResponse edit(@ModelAttribute("goonForm") GoonSaveWrapper wrapper, BindingResult result)
	{
		System.out.println("Mode:" + wrapper.getMode() + ", Name:" + wrapper.getDetails().getName());  
		boolean success = dataConnection.saveGoonDetails(wrapper.getDetails());
		String resp = success ? PostResponse.SUCCESS : PostResponse.FAILURE;
		return new PostResponse(resp);
	}
	
	//, consumes="application/json"
	@RequestMapping(headers ={"Accept=application/json"}, value = "/editJson", method = RequestMethod.POST, consumes="application/json")
	public @ResponseBody PostResponse editJSON(@RequestBody GoonSaveWrapper form, Model model) {  
		System.out.println("JSON Mode:" + form.getMode() + ", Name:" + form.getDetails().getName());  
		boolean success = dataConnection.saveGoonDetails(form.getDetails());
		String resp = success ? PostResponse.SUCCESS : PostResponse.FAILURE;
		return new PostResponse(resp);
	}
}
